package com.jy.adscm.serviceProvider.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.bean.DealerServiceRelation;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.dealer.mapper.DealerServiceRelationMapper;
import com.jy.adscm.dealer.service.DealerService;
import com.jy.adscm.hospital.bean.JyHospitalBase;
import com.jy.adscm.hospital.mapper.JyHospitalBaseMapper;
import com.jy.adscm.serviceProvider.bean.JyService;
import com.jy.adscm.serviceProvider.bean.JyServiceChart;
import com.jy.adscm.serviceProvider.mapper.JyServiceMapper;
import com.jy.adscm.serviceProvider.service.JyServiceChartService;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/1/27 -11:30
 */
@Service
public class JyServiceChartServiceImpl  implements JyServiceChartService {
     @Autowired
    JyHospitalBaseMapper jyHospitalBaseMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;
     @Autowired
    DealerMapper dealerMapper;
     @Autowired
    DealerServiceRelationMapper dealerServiceRelationMapper;
    @Autowired
    JyServiceMapper jyServiceMapper;
    @Override
    public List<JyServiceChart> selectServiceChart(String serviceType, String startTime, String endTime) {
        List<JyServiceChart> jyServiceCharts =new ArrayList<>();

        //查询未审核的服务
        Wrapper<JyService> wrapper =new EntityWrapper<>();
        wrapper.eq("service_type",serviceType);
        wrapper.eq("service_state",0);
        wrapper.ge("service_need_time",startTime);
        wrapper.le("service_need_time",endTime);
        List<JyService> jyServices = jyServiceMapper.selectList(wrapper);
        for (JyService jyService : jyServices) {
            JyServiceChart jyServiceChart=new JyServiceChart();
            jyServiceChart.setId(jyService.getId());
            jyServiceChart.setApplicantTime(jyService.getApplicantTime());
            jyServiceChart.setServiceDate(jyService.getServiceNeedTime());
            jyServiceChart.setState("0");
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyService.getDealer());
                jyServiceChart.setDealer(dealer.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getHospital(),null)){
                JyHospitalBase jyHospitalBase = jyHospitalBaseMapper.selectById(jyService.getHospital());
                jyServiceChart.setHospital(jyHospitalBase.getHospitalName());
                jyServiceChart.setLongitude(jyHospitalBase.getHospitalJd());
                jyServiceChart.setLatitude(jyHospitalBase.getHospitalWd());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDepartment(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setCode(jyService.getDepartment());
                jyCode.setType(4);
                jyCode = jyCodeMapper.selectOne(jyCode);
                jyServiceChart.setDepartment(jyCode.getName());
            }
            jyServiceCharts.add(jyServiceChart);
        }
        // 查询已经审核的
        Wrapper<JyService> wrapper1 =new EntityWrapper<>();
        wrapper1.eq("service_type",serviceType);
        wrapper1.eq("service_state",1);
        wrapper1.ge("sure_service_time",startTime);
        wrapper1.le("sure_service_time",endTime);
        List<JyService> jyServices1 = jyServiceMapper.selectList(wrapper1);
        for (JyService jyService : jyServices1) {
            JyServiceChart jyServiceChart=new JyServiceChart();
            jyServiceChart.setId(jyService.getId());
            jyServiceChart.setApplicantTime(jyService.getApplicantTime());
            jyServiceChart.setServiceDate(jyService.getSureServiceTime());
            jyServiceChart.setState("1");
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyService.getDealer());
                jyServiceChart.setDealer(dealer.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getHospital(),null)){
                JyHospitalBase jyHospitalBase = jyHospitalBaseMapper.selectById(jyService.getHospital());
                jyServiceChart.setHospital(jyHospitalBase.getHospitalName());
                jyServiceChart.setLongitude(jyHospitalBase.getHospitalJd());
                jyServiceChart.setLatitude(jyHospitalBase.getHospitalWd());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyService.getDepartment(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setCode(jyService.getDepartment());
                jyCode.setType(4);
                jyCode = jyCodeMapper.selectOne(jyCode);
                jyServiceChart.setDepartment(jyCode.getName());
            }
            jyServiceCharts.add(jyServiceChart);
        }
        //查询对应有这个服务的供应商
        Wrapper<DealerServiceRelation>  dealerServiceRelationWrapper =new EntityWrapper<>();

        dealerServiceRelationWrapper.eq("service_id",serviceType);
        List<DealerServiceRelation> dealerServiceRelations = dealerServiceRelationMapper.selectList(dealerServiceRelationWrapper);
        for (DealerServiceRelation dealerServiceRelation : dealerServiceRelations) {
            JyServiceChart jyServiceChart=new JyServiceChart();
            jyServiceChart.setState("2");
            if (AdscmBaseUtils.getInstance().isNotEmpty(dealerServiceRelation.getDid(),null)){
                Dealer dealer = dealerMapper.selectById(dealerServiceRelation.getDid());
                jyServiceChart.setId(dealer.getId());
                jyServiceChart.setDealer(dealer.getName());
                jyServiceChart.setLatitude(dealer.getLatitude());
                jyServiceChart.setLongitude(dealer.getLongitude());
            }
            jyServiceCharts.add(jyServiceChart);
        }
        return jyServiceCharts;
    }
}
